//控制层 
app.controller('typeController', function($scope, $location, $controller, typeService) {

	$controller('baseController', {
		$scope: $scope
	}); //继承

	//读取列表数据绑定到表单中  
	$scope.findAll = function() {
		typeService.findAll().success(
			function(response) {
				$scope.list = response;
			}
		);
	}

	//分页
	$scope.findPage = function(page, rows) {
		typeService.findPage(page, rows).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}
	//上传图片并保存课程
	$scope.uploadAndSave = function() {
		//提交课程前先保存图片到数据库,并拿到相对路径
		if($scope.entity.image != null) {
			typeService.upload().then(function(response) {
				//保存图片后进行课程信息和图片路径的保存
				$scope.entity.icon = response.data.data.src;
				$scope.save();
			});
		} else {
			$scope.save();
		}
	}

	//查询实体 
	$scope.findOne = function(id) {
		typeService.findOne(id).success(
			function(response) {
				$scope.entity = response;
			}
		);
	}

	//编辑页面携带参数
	$scope.edit = function() {

		var id = $location.absUrl().split("?id=")[1];
		$scope.findOne(id);
	}

	//保存 
	$scope.save = function() {
		var serviceObject; //服务层对象  				
		if($scope.entity.id != null) { //如果有ID
			serviceObject = typeService.update($scope.entity); //修改  
		} else {
			var cate = $location.absUrl().split("?cateId=")[1];
			$scope.entity.cateId = cate;
			serviceObject = typeService.add($scope.entity); //增加 
		}
		serviceObject.success(
			function(response) {
				if(response.success) {
					alert(response.message);
					//重新查询 
					window.history.go(-1);
				} else {
					alert(response.message);
				}
			}
		);
	}

	//单个删除
	$scope.delsFlag = false;
	$scope.delsingle = function(id) {
		if(confirm('确实要删除该图片吗?')) {
			typeService.dele(id).success(
				function(response) {
					if(response.success) {
						alert(response.message);
						$scope.reloadList();
						$scope.delsFlag = true;
						$timeout(function() {
							$scope.delsFlag = false;
						}, 2000);
					} else {
						alert(response.message);
					}
				}
			);
		}

	}

	//选中全部
	$scope.selectAll = function($event) {
		if($event.target.checked) { //如果是被选中,则增加到数组
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				$scope.selectIds.push(each.id); //将空数组中push新的数据，符合条件的数据的id值
			});
		} else {
			angular.forEach($scope.list, function(each) { //foreach循环，datalist是需要循环的数据列表，function后面需要加each
				var idx = $scope.selectIds.indexOf(each.id);
				$scope.selectIds.splice(idx, 1); //删除 
			});
		}
	}

	//批量删除 
	$scope.dele = function() {
		if($scope.selectIds.length < 1) {
			alert("请选中要删除的图片!");
		} else {
			if(confirm('确实要删除 ' + $scope.selectIds.length + ' 张图片吗?')) {
				//获取选中的复选框			
				typeService.dele($scope.selectIds).success(
					function(response) {
						if(response.success) {
							alert(response.message);
							$scope.reloadList(); //刷新列表
							$scope.selectIds = [];
						} else {
							alert(response.message);
						}
					}
				);
			}
		}
	}

	$scope.searchEntity = {}; //定义搜索对象 

	//搜索
	$scope.search = function(page, rows) {
		typeService.search(page, rows, $scope.searchEntity).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}
	$scope.addImage = function() {
		angular.element('#imageShow').click();
	}

});